package com.tripwego.client.ui.pageVoyage.cr;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DateLabel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.tripwego.client.common.TwgConstants;
import com.tripwego.client.event.ExpandCollapseEvent;
import com.tripwego.client.ui.pageVoyage.IVoyageView;

public class CRHeaderWidget extends Composite implements IVoyageView, ClickHandler, HasMouseOverHandlers, HasMouseOutHandlers {

	private Presenter presenter;

	private EventBus eventBus;

	/*
	 * Elements graphiques
	 */
	private FlexTable viewPanel = new FlexTable();

	private Image image_collapse = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/minimize.png");

	private Image image_expand = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/maximize.png");

	private final HTML html_title = new HTML("Mon voyage", false);

	private HorizontalPanel panel_actions = new HorizontalPanel();

	private DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat(TwgConstants.FORMAT_DATE_LODGING);

	private final DateLabel dateLabel_dateStart = new DateLabel(dateTimeFormat);

	/**
	 * 
	 */
	public CRHeaderWidget() {
		super();

		viewPanel.setStyleName("CRHeaderWidget");
		viewPanel.setSize("350px", "");

		initWidget(viewPanel);

		html_title.setStyleName("CRHeaderWidget_html_title");

		dateLabel_dateStart.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		dateLabel_dateStart.setStyleName("lblDateDay");
		dateLabel_dateStart.setWordWrap(false);

		viewPanel.setWidget(0, 0, dateLabel_dateStart);

		viewPanel.setWidget(0, 1, html_title);
		viewPanel.getCellFormatter().setWidth(0, 1, "100%");
		panel_actions.setStyleName("horizontalPanel_actions");
		panel_actions.setSpacing(2);

		viewPanel.setWidget(0, 2, panel_actions);
		panel_actions.setWidth("1%");

		panel_actions.add(image_collapse);
		image_collapse.setTitle("Replier");

		panel_actions.add(image_expand);
		image_expand.setTitle("Deplier");

		viewPanel.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
		viewPanel.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);

		image_collapse.setStyleName("img_action");
		image_expand.setStyleName("img_action");
		viewPanel.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);

		addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				majPanelActions(false);
			}
		});

		addMouseOutHandler(new MouseOutHandler() {

			@Override
			public void onMouseOut(MouseOutEvent event) {
				majPanelActions(true);
			}
		});

		image_collapse.addClickHandler(this);
		image_expand.addClickHandler(this);
	}

	@Override
	public void onClick(ClickEvent event) {
		Object sender = event.getSource();

		if (sender == image_collapse) {
			eventBus.fireEvent(new ExpandCollapseEvent(false));
		}

		if (sender == image_expand) {
			eventBus.fireEvent(new ExpandCollapseEvent(true));
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	@Override
	public void init() {
		html_title.setHTML(presenter.getTripName());

		majPanelActions(true);
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	public void majPanelActions(boolean disabled) {
		image_collapse.setStyleDependentName("disabled", disabled);
		image_expand.setStyleDependentName("disabled", disabled);
	}
}
